package TCP.TCP6;

import javafx.scene.SnapshotParameters;
import jdk.internal.util.xml.impl.Input;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient {
    private Socket socket;
    public TcpClient() throws IOException {
        //建立连接
        socket = new Socket("127.0.0.1", 8080);
    }
    public void start() {
        Scanner in = new Scanner(System.in);
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()) {
            PrintWriter printWriter = new PrintWriter(outputStream);
            Scanner inSocket = new Scanner(inputStream);
            while(true) {
                //1.从控制台读入请求
                System.out.print("->");
                String request = in.next();
                //2.将请求发送给服务器
                printWriter.println(request);
                //刷新缓冲区
                printWriter.flush();
                //3.接收服务器响应
                String response = inSocket.next();
                //4.打印响应
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpClient tcpClient = new TcpClient();
        tcpClient.start();
    }
}
